<?php
/**
 * HTTP客户端
*/

namespace houze\client;

class Http
{
    /**
     * 域名
    */
    private $domain;
    /**
     * API地址
    */
    private $api_url;
    /**
     * 端口
    */
    private $port = 80;
    /**
     * 请求内容
    */
    private $data = null;
    /**
     * 客户端实例
    */
    private $client_class;
    private $client;
    /**
     * 代理存储 
     * 格式："http://username:password@192.168.16.1:10"
    */ 
    private $proxy = null;
    
    /**
     * 以下为client返回的信息
    */
    private $errCode;
    private $statusCode;
    private $body;
    private $headers;
    private $cookies;

    /**
     *  发送请求 - get
    */
    public function get() {
        // 设置请求参数
        if (!empty($this->data)) {
            $this->api_url .= '?'.http_build_query($this->data);
        }
        if(empty($this->proxy)){
            $set = [
                'verify'    =>  false,
            ];
        }else{
            $set = [
                "proxy" => $this->proxy,
                'verify'    =>  false,
            ];
        }
        
        // 发送请求
        $result = $this->client->request('GET', $this->api_url, $set);
        return $result->getBody()->__toString();
    }
    /**
     * 发送请求 - post
    */
    public function post() {
        if(empty($this->proxy)){
            $set = [
                'verify'    =>  false,
            ];
        }else{
            $set = [
                "proxy" => $this->proxy,
                'verify'    =>  false,
            ];
        }
        // 发送请求
        $result = $this->client->request('POST', $this->api_url, $this->data, $set);
        return $result->getBody()->__toString();
    }
    
    /**
     * 注入请求地址
    */
    public function domain($url) {
        $array = parse_url($url);
        // 域名
        if (isset($url)) {
            $this->domain = $url;
        }
        // API地址
        if ($this->domain) {
            $this->api_url = $this->domain;
        }else{
            $this->api_url = '/';
        }
        if (isset($array['query'])) {
            $this->api_url .= '?'.$array['query'];
        }

        // 创建客户端实例
        $this->client_class = new \ReflectionClass('\GuzzleHttp\Client');
        $this->client = $this->client_class->newInstanceArgs();
        return $this;
    }
    /**
     * 注入请求内容
    */
    public function body($data=null) {
        $this->data = $data;
        return $this;
    }
    /**
     * 注入请求代理
    */
    public function proxy($data=null) {
        $this->proxy = $data;
        return $this;
    }
    /**
     * 获取errCode
    */
    public function errCode(){ return $this->errCode;}
    /**
     * 获取statusCode
    */
    public function statusCode(){ return $this->statusCode;}
    /**
     * 获取headers
    */
    public function headers(){ return $this->headers;}
    /**
     * 获取cookies
    */
    public function cookies(){ return $this->cookies;}
    /**
     * 获取响应头
    */
    public function getHeaders(){ return $this->client->getHeaders();}
    /**
     * 反射类实现调用Client本身的其他支持方法
    */
    public function __call($name, $arguments=[]) {
        if (!$this->client) return false;
        if (empty($name)) return false;
        if (!$this->client_class->hasMethod($name)) return false;

        $obj = $this->client_class->getmethod($name);
        $obj->invokeArgs($this->client, $arguments);
        return $this;
    }
}